class_name Addsys
extends System

var CATCH_READ = ["SkillLibC", "SkillCmdC", "DelaySkillsC", "CampC", "DataC", "SkillLib"]
var catch_read: Array


func _init():
	system_name = "Addsys"
	listen_create("add", "Entities", ["Entities"])
	# listen_modify("listen", "AddCmd", ["AddCmd"])


func _system_ready():
	catch_read = world.query_manager.fetch_components(CATCH_READ)


#@listenCreate(Entities)
#read:[Single<Entities>]
func add(e, read: Array):
	var id = e.id
	var entity = read[0].get(id) as Entity
	init_entity(id, entity.data)


# func listen(_e, read: Array):
# 	var add_cmd = read[0].data as AddCmd
# 	print("add cmd:", add_cmd.value)
# 	var arr = add_cmd.value
# 	if arr:
# 		for r in arr:
# 			init_entity(1, r)


func init_entity(id, cmd):
	var camp_m = catch_read[3]
	var data_m = catch_read[4]

	var data = DataC.new()
	data.attack = 0
	data.sprite_name = cmd.sprite
	data_m.insert(id, data)

	var camp = CampC.new()
	camp.value = cmd.camp
	camp_m.insert(id, camp)

	init_fighter_skill(id, cmd)


func init_fighter_skill(id, data):
	if data.skills:
		var skill_lib = SkillLibC.new()
		var skill_cmd = SkillCmdC.new()
		catch_read[0].insert(id, skill_lib)
		catch_read[1].insert(id, skill_cmd)
		catch_read[2].insert(id, DelaySkillsC.new())
		#
		var SkillLib: SkillLib = catch_read[5].data
		var Skill = SkillCmdC.Skill
		for skill_id in data.skills:
			var model = SkillLib.map.get(skill_id)
			var sk = Skill.new(model)
			skill_lib.map[skill_id] = sk
			if sk.get("skillCastType") == "passive":
				skill_cmd.value = SkillCmdC.SkillCmd.new(skill_id)
